/**
 * Created by ZFL on 2017/12/21.
 */
/**
 * Created by admin on 2017/1/20.
 */
/**
 * Created by admin on 2016/11/29.
 */
function PageInit() {
    infinite();
    getGoods();
}

//加载页面信息
var page = 1;
var pageSize = 10;
var loading = false;
function getGoods() {
    var _visible = 'show';
    if (loading) {
        _visible = 'hide';
    }
    $.ajaxHandler({
        model: function () {
            var obj = {};
            if (com_obj.userId) {
                obj.userId = com_obj.userId;
            } else {
                obj.userId = 0;
            }
            obj.pageNum = page;
            obj.pageSize = pageSize;
            return obj;
        },
        visible: '' + _visible + '',
        visibleTime: 500,
        approve: true,
        api: "yg_baijiaapp/serial/moreNomalSrials",
        success: function (result) {
            console.log(result);
            if (result.code == 10000) {
                var data = result.data.list;
                if (data.length > 0) {
                    var _data = [];
                    var _obj = {};
                    $.each(data, function (index, item) {
                        var cobj = {};
                        if (item.coverImgpath.toString().indexOf("http") <= -1) {
                            cobj.coverImgpath = $.imgApi() + item.coverImgpath;
                        } else {
                            cobj.coverImgpath = item.coverImgpath;
                        }
                        cobj.title = item.title;
                        cobj.seeCount = item.seeCount;
                        cobj.goodsNum = item.goodsNum;
                        cobj.serialId = item.serialId;
                        var list = [];
                        $.each(item.goodsList, function (cindex, citem) {
                            var obj2 = {};
                            if (citem.goodsImage.toString().indexOf("http") <= -1) {
                                obj2.goodsImage = $.imgApi() + '' + citem.goodsImage;
                            } else {
                                obj2.goodsImage = citem.goodsImage;
                            }
                            obj2.goodsId = citem.goodsId;
                            obj2.name = citem.name;
                            obj2.lowestPrice = parseFloat(divideNumber(citem.lowestPrice, 100));
                            obj2.suggestedPrice = parseFloat(divideNumber(citem.suggestedPrice, 100));
                            obj2.priceShow=true;
                            if(citem.lowestPrice>=citem.suggestedPrice){
                                obj2.priceShow=false;
                            }
                            list.push(obj2);
                        });
                        cobj.goodsList = list;
                        var mWidth = 9.2;
                        var _width = 2.7466 * (item.goodsList.length + 1) + 0.2 * (item.goodsList.length + 2);
                        if (_width > mWidth) {
                            mWidth = _width;
                        }
                        cobj.width = mWidth;
                        _data.push(cobj);
                    });
                    _obj.data = _data;
                    var source = $("#template").html();
                    var template = Handlebars.compile(source);
                    $(".recom").prepend(template(_obj));
                    for(var i=0; i<data.length; i++){
                        for(var j=0; j<data[i].goodsList.length; j++){
                            if(data[i].goodsList[j].goodsChannel==3){
                                $(".goods-pic").show();
                            }else {
                                $(".goods-pic").hide();
                            }
                        }
                    }
                    if (loading) {
                        $('.show-loading').hide();
                        loading = false;
                    }
                } else {
                    if (loading) {
                        $('.show-loading').remove();
                        $.toast('没有数据了~', "text");
                        $.detachInfiniteScroll($('.infinite-scroll'));
                        $('.infinite-scroll-preloader').remove();
                    }
                }

            } else {
                $.toast("加载期异常", "text");
            }
        }
    });
}

//滚动加载
function infinite() {
    $(document).on('infinite', '.infinite-scroll', function () {
        // 如果正在加载，则退出
        if (loading) return;
        // 设置flag
        loading = true;
        $('.show-loading').show();
        setTimeout(function () {
            page++;
            getGoods()
        }, 1000);
    });
}